import axios from "axios";
import { urls } from "./url.js";
import { baseUrl } from "@/utils/env";

const fetch = (name, data) => {
  let urlList = urls();
  return new Promise((resolve, reject) => {
    let url = urlList[name] || "";
    if (!url) reject("url未添加 请先添加请求地址到url.js");
    url = baseUrl + url;
    axios
      .post(url, {
        ...data,
      })
      .then((res) => {
        if (res.data.code !== 200) {
          console.log("fetch err", res.data);
          reject(res.data);
        } else resolve(res.data.data);
      })
      .catch((err) => {
        console.log("fetch err", err.data || err);
        reject(err.data || err);
      })
      .finally(() => {
        console.log("fetch finally url:" + url);
      });
  });
};

export default fetch;
